package com.balance.anole.client.common.dao.hibernate;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;

import com.balance.anole.business.to.ProductItemTO;
import com.balance.anole.business.vo.ProductItemSearchTO;
import com.balance.anole.client.common.dao.ProductItemDAO;
import com.balance.anole.erp.common.model.ProductItemVO;

public class ProductItemDAOImpl extends BaseDAOImpl implements ProductItemDAO {

	@SuppressWarnings("unchecked")
	public ProductItemTO getProductItemTOByItemId(String itemId) {
		List<ProductItemTO> l = getHibernateTemplate().find("from " + ProductItemTO.class.getName() + " as pi where pi.itemId = ?",itemId);
		if(l != null && l.size() > 0){
			return (ProductItemTO)l.get(0);
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ProductItemVO> getProductItems(final ProductItemSearchTO searchTO) {

		String hql = "select new "
				+ ProductItemVO.class.getName()
				+ "(pi.sn, pi.productTO.pdtDesc, col, size) from "
				+ ProductItemTO.class.getName()
				+ " pi inner join pi.colorTO col inner join pi.sizeTO size where 1=1 ";

		if(searchTO.getProductItemId()!=null && !"".equals(searchTO.getProductItemId())){
			hql += " and pi.sn = :sn";
		}

		if(searchTO.getProductDesc()!=null && !"".equals(searchTO.getProductDesc())){
			hql += " and pi.productTO.pdtDesc like :pdtDesc";
		}
		if(searchTO.getStyleSn()!=null && !"".equals(searchTO.getStyleSn())){
			hql += " and pi.productTO.styleId = :styleId";
		}

		if(searchTO.getProductTypeId() !=null && ! StringUtils.isEmpty(searchTO.getProductTypeId().getCodeId())){
			hql += " and pi.productTO.pdtGroupTO.productType.codeId = :productTypeId";
		}

		Query query = getSession().createQuery(hql);
		if(searchTO.getProductItemId()!=null && !"".equals(searchTO.getProductItemId())){
			query.setParameter("sn", searchTO.getProductItemId());
		}

		if(searchTO.getProductDesc()!=null && !"".equals(searchTO.getProductDesc())){
			query.setParameter("pdtDesc", "%" + searchTO.getProductDesc() + "%");
		}
		if(searchTO.getStyleSn()!=null && !"".equals(searchTO.getStyleSn())){
			query.setParameter("styleId", searchTO.getStyleSn());
		}

		if(searchTO.getProductTypeId() !=null && ! StringUtils.isEmpty(searchTO.getProductTypeId().getCodeId())){
			query.setParameter("productTypeId", searchTO.getProductTypeId().getCodeId());
		}


		return query.list();

		/*DetachedCriteria dc = DetachedCriteria.forClass(ProductItemTO.class);
		if(searchTO.getProductItemId()!=null && !"".equals(searchTO.getProductItemId())){
			dc.add(Restrictions.eq("sn", searchTO.getProductItemId()));
		}

		DetachedCriteria sub = dc.createCriteria("productTO");
		if(searchTO.getProductDesc()!=null && !"".equals(searchTO.getProductDesc())){
			sub.add(Restrictions.ilike("pdtDesc", searchTO.getProductDesc(), MatchMode.ANYWHERE));
		}
		if(searchTO.getStyleSn()!=null && !"".equals(searchTO.getStyleSn())){
			sub.add(Restrictions.eq("styleId", searchTO.getStyleSn()));
		}

		if(searchTO.getProductTypeId() !=null && ! StringUtils.isEmpty(searchTO.getProductTypeId().getCodeId())){
			DetachedCriteria pdtGroupTOSub = sub.createCriteria("pdtGroupTO");
			DetachedCriteria codeSub = pdtGroupTOSub.createCriteria("productType");
			codeSub.add(Restrictions.eq("codeId", searchTO.getProductTypeId().getCodeId()));
		}

		List<ProductItemTO> to = getHibernateTemplate().findByCriteria(dc);
		List<ProductItemVO> result = new ArrayList<ProductItemVO>();
		if(to != null){
			for (ProductItemTO productItemTO : to) {
				ProductItemVO vo = new ProductItemVO(productItemTO.getSn(), productItemTO.getProductTO().getPdtDesc(), productItemTO.getColorTO(), productItemTO.getSizeTO());
				result.add(vo);
			}
		}

		return result;*/
	}

	@SuppressWarnings("unchecked")
	@Override
	public ProductItemTO getProductItemTOBySN(String sn) {
		String hql = "from " + ProductItemTO.class.getName() + " as pi where pi.sn = :sn";
		Query query = getSession().createQuery(hql);
		query.setParameter("sn", sn);
//		List<ProductItemTO> l = getHibernateTemplate().find("from com.balance.anole.admin.to.ProductItemTO as pi where pi.sn = ?",sn);
		List<ProductItemTO> l = query.list();
		if(l != null && l.size() > 0){
			return (ProductItemTO)l.get(0);
		}
		return null;
	}

}
